﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace IM.Easy.Core.Extensions
{
    public static class StringExtension
    {
        public static long ToLong(this object obj)
        {
            if (obj == null)
                return 0;

            try
            {
                return Convert.ToInt64(Convert.ToDouble(obj));
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// 转换为帕斯卡命名
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToPascalCase(this string input)
        {
            if (string.IsNullOrEmpty(input))
                return string.Empty;

            // 如果输入本身已经是 PascalCase，则直接返回
            if (!input.Contains('_') && char.IsUpper(input[0]) && !input.Any(char.IsWhiteSpace))
                return input;

            // 将下划线分割的字符串转换为 PascalCase
            var words = input.Split('_', StringSplitOptions.RemoveEmptyEntries);
            var pascalCaseString = string.Concat(words.Select(word => char.ToUpper(word[0]) + word[1..].ToLower()));

            return pascalCaseString;
        }

        /// <summary>
        /// 转换为驼峰命名
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToCamelCase(this string input)
        {
            if (string.IsNullOrWhiteSpace(input))
                return input;

            // 按空格、下划线或连字符分隔字符串并去除空白项
            var words = input.Split(new[] { ' ', '_', '-' }, StringSplitOptions.RemoveEmptyEntries);

            // 首个单词全部小写，其他单词首字母大写
            for (int i = 0; i < words.Length; i++)
            {
                words[i] = i == 0
                    ? char.ToLowerInvariant(words[i][0]) + words[i][1..]
                    : char.ToUpperInvariant(words[i][0]) + words[i][1..].ToLower();
            }

            return string.Concat(words);
        }
    }


}
